package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorFriendshipLink;
import guoanmaker.operator.business.service.OperatorFriendshipLinkServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorFriendshipLinkController
 * @Description: OperatorFriendshipLinkController 
 * @date 2017/6/28 上午11:10 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_FRIENDSHIPLINK_URL)
public class OperatorFriendshipLinkController {
    private final OperatorFriendshipLinkServiceInterface operatorFriendshipLinkServiceInterface;

    public OperatorFriendshipLinkController( OperatorFriendshipLinkServiceInterface operatorFriendshipLinkServiceInterface) {
        this.operatorFriendshipLinkServiceInterface = operatorFriendshipLinkServiceInterface;
    }

    /** 
    * @Title: saveFriendshipLink
    * @Description: 新增/修改友情链接 
    * @param: OperatorFriendshipLink   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存友情链接", notes = "新增/修改友情链接")
    @PostMapping(value = "saveFriendshipLink")
    public AjaxJson saveFriendshipLink(@RequestBody InRoundPhone inRoundPhone) {
    	AjaxJson ajaxJson=new AjaxJson();
    	OperatorFriendshipLink[] pps=inRoundPhone.getOperatorFriendshipLinks();
    	
    	try{
    		operatorFriendshipLinkServiceInterface.delectAllBycitycode(inRoundPhone.getType());
    	for(OperatorFriendshipLink pp:pps){
    		pp.setCreateDate(new Date());
    		pp.setType(inRoundPhone.getType());
    		operatorFriendshipLinkServiceInterface.saveFriendshipLink(pp);
    	}
    	ajaxJson.setSuccess(true).setStatus(200);
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250);
    	}
    	
        return ajaxJson;
    }

    /** 
     * @Title: deleteFriendshipLinkById
     * @Description: 根据Id删除友情链接
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除友情链接", notes = "根据Id删除友情链接")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteFriendshipLinkById(@PathVariable(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
    	operatorFriendshipLinkServiceInterface.deleteFriendshipLinkById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorFriendshipLink    
     * @Title: getFriendshipLinkById 
     * @Description: 根据Id查询友情链接
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询友情链接", notes = "根据Id查询友情链接")
    @GetMapping(value = "getFriendshipLinkById")
    public AjaxJson getFriendshipLinkById(@RequestParam(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorFriendshipLinkServiceInterface.getFriendshipLinkById(id));
    }

    /**type 1是首页 2是登录页面
     * @return AjaxJson   
     * @Title: getAllFriendshipLink
     * @Description: 查找所有友情链接 
     * @throws 
     */
    @ApiOperation(value = "查找所有友情链接", notes = "查找所有友情链接")
    @PostMapping(value = "getAllFriendshipLink")
    public AjaxJson getAllFriendshipLink(String type) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	List<OperatorFriendshipLink> feiendships=operatorFriendshipLinkServiceInterface.getAllFriendshipLink(type);
    	ajaxJson.setSuccess(true).setStatus(200).setData(feiendships);
    	}catch(Exception e){
    		ajaxJson.setSuccess(false).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: getAllFriendshipLinkByPage 
     * @Description: 查询所有友情链接并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询友情链接", notes = "分页查询友情链接")
    @GetMapping(value = "getAllFriendshipLinkByPage")
    public AjaxJson getAllFriendshipLinkByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(operatorFriendshipLinkServiceInterface.getAllFriendshipLinkByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllFriendshipLinkByWeightAndPage 
     * @Description: 按权重排序分页查询友情链接 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询友情链接", notes = "按权重排序分页查询友情链接 ")
    @GetMapping(value = "getAllFriendshipLinkByWeightAndPage")
    public AjaxJson getAllFriendshipLinkByWeightAndPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(operatorFriendshipLinkServiceInterface.getAllFriendshipLinkByWeightAndPage(page, size));
    }
    
    /**
     * 
    * @Title: getAllFriendshipLinkByCityCode 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param cityCode
    * @return    设定文件 
    * @return AjaxJson    返回类型 
    * @throws 
     */
    @ApiOperation(value = "根据城市code查找所有友情链接", notes = "根据城市code查找所有友情链接")
    @RequestMapping(value = "/getAllFriendshipLinkByCityCode", method = RequestMethod.POST)
    public AjaxJson getAllFriendshipLinkByCityCode(String cityCode) {
    	AjaxJson ajaxJson=new AjaxJson();
    	List<OperatorFriendshipLink> ofls=operatorFriendshipLinkServiceInterface.getAllFriendshipLinkByCityCode(cityCode);
    	if(ofls.size()!=0){
    		ajaxJson.setSuccess(true).setStatus(200).setData(ofls);
    	}else{
    		ajaxJson.setSuccess(false).setStatus(250).setMsg("查询失败");
    	}
        return ajaxJson;
    }

}
